// 引入搜索商品ajax
import { reqGetSearchInfo } from '@/api'

// search仓库的小模块
export default {
  actions: {
    // 传入的params至少是个空对象
    async getSearchList ({ commit }, params = {}) {
      const result = await reqGetSearchInfo(params);
      if (result.code >= 200 && result.code < 300) {
        commit('SEARCHLIST', result.data);
      }
    }
  },
  mutations: {
    SEARCHLIST (state, searchList) {
      state.searchList = searchList;
    }
  },
  state: {
    searchList: {}
  },
  // getter的用途：常用于简化仓库的数据
  getters: {
    // state形参指的是当前这个局部store的state对象
    goodsList (state) {
      // 要保证传入的是数组，即使数组是空的，否则会引发一些问题(undefined的值不能遍历，会引发控制台报错)
      return state.searchList.goodsList || []
    },
    attrsList (state) {
      return state.searchList.attrsList || []
    },
    trademarkList (state) {
      return state.searchList.trademarkList || []
    }
  }
}
